package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.*;
import org.csu.mypetstore.api.persistence.LineItemMapper;
import org.csu.mypetstore.api.persistence.OrderMapper;
import org.csu.mypetstore.api.persistence.SequenceMapper;
import org.csu.mypetstore.api.entity.Cart;
import org.csu.mypetstore.api.entity.Sequence;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.service.OrderService;
import org.csu.mypetstore.api.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SequenceMapper sequenceMapper;

    @Autowired
    private LineItemMapper lineItemMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private CatalogService catalogService;

//    @Autowired
//    private LineItemMa

    @Autowired
    private CartService cartService;

    @Override
    public CommonResponse<OrderVO> setCartSession(HttpSession session,Order order){
        Cart cart = (Cart)session.getAttribute("cart");
        System.out.println(cart);
        if(cart == null){
            return CommonResponse.createForError("购物车为空");
        }
        else
        {
            order.setTotalPrice(cart.getSubTotal());
            OrderVO orderVO = new OrderVO();
            orderVO.setCardType(order.getCardType());
            orderVO.setCardNumber(order.getCreditCard());
            orderVO.setOrderDate(order.getOrderDate());
            orderVO.setBillFirstName(order.getBillToFirstName());
            orderVO.setBillLastName(order.getBillToLastName());
            orderVO.setBillAddress1(order.getBillAddress1());
            orderVO.setBillAddress2(order.getBillAddress2());
            orderVO.setBillCity(order.getBillCity());
            orderVO.setBillState(order.getBillState());
            orderVO.setBillZip(order.getBillZip());
            orderVO.setBillCountry(order.getBillCountry());
            orderVO.setShipFirstName(order.getShipToFirstName());
            orderVO.setShipLastName(order.getShipToLastName());
            orderVO.setShipAddress1(order.getShipAddress1());
            orderVO.setShipAddress2(order.getShipAddress2());
            orderVO.setShipCity(order.getShipCity());
            orderVO.setShipState(order.getShipState());
            orderVO.setShipZip(order.getShipZip());
            orderVO.setShipCountry(order.getShipCountry());
            orderVO.setUserId(order.getUsername());
            orderVO.setTotal(cart.getSubTotal());

            session.setAttribute("orderVO",orderVO);
            return CommonResponse.createForSuccessMessage("添加Session成功");
        }
    }

    @Override
    public CommonResponse<OrderVO> setCartShipSession(HttpSession session,Order order){
        OrderVO orderVO = (OrderVO) session.getAttribute("orderVO");
        if(orderVO == null){
            return CommonResponse.createForError("购物车为空");
        }
        else
        {
            orderVO.setShipFirstName(order.getShipToFirstName());
            orderVO.setShipLastName(order.getShipToLastName());
            orderVO.setShipAddress1(order.getShipAddress1());
            orderVO.setShipAddress2(order.getShipAddress2());
            orderVO.setShipCity(order.getShipCity());
            orderVO.setShipState(order.getShipState());
            orderVO.setShipZip(order.getShipZip());
            orderVO.setShipCountry(order.getShipCountry());
            orderVO.setUserId(order.getUsername());
            session.setAttribute("orderVO",orderVO);
            return CommonResponse.createForSuccessMessage("添加Session成功");
        }
    }

    @Override
    public CommonResponse<OrderVO> getCartSession(HttpSession session){
        OrderVO orderVO = (OrderVO) session.getAttribute("orderVO");
        if(orderVO == null){
            return CommonResponse.createForError("购物车为空");
        }
        else{
            return CommonResponse.createForSuccess(orderVO);
        }

    }

    @Override
    public CommonResponse<OrderVO> insertOrder(HttpSession session){
        //取得获得购物车的数据
        CommonResponse<CartVO> response = cartService.viewCart(session);
        CartVO cartVO = response.getData();

        Order order = new Order();

        AccountVO accountVO = accountService.getAccount((String)session.getAttribute("username")).getData();
        order.setUsername(accountVO.getUsername());
        OrderVO orderVO = (OrderVO) session.getAttribute("orderVO");
        order.setOrderDate(orderVO.getOrderDate());
        order.setShipAddress1(orderVO.getShipAddress1());
        order.setShipAddress2(orderVO.getShipAddress2());
        order.setShipCity(orderVO.getShipCity());
        order.setShipState(orderVO.getShipState());
        order.setShipZip(orderVO.getShipZip());
        order.setShipCountry(orderVO.getShipCountry());
        order.setBillAddress1(orderVO.getBillAddress1());
        order.setBillAddress2(orderVO.getBillAddress2());
        order.setBillCity(orderVO.getBillCity());
        order.setBillState(orderVO.getBillState());
        order.setBillZip(orderVO.getBillZip());
        order.setBillCountry(orderVO.getBillCountry());
        order.setCourier("UPS");
        order.setTotalPrice(cartVO.getTotalCost());
        order.setBillToFirstName(orderVO.getBillFirstName());
        order.setBillToLastName(orderVO.getBillLastName());
        order.setShipToFirstName(orderVO.getShipFirstName());
        order.setShipToLastName(orderVO.getShipLastName());
        order.setCreditCard(orderVO.getCardNumber());
        order.setExpiryDate(orderVO.getExpiryDate());
        order.setCardType(orderVO.getCardType());
        order.setLocale(orderVO.getLocale());
        order.setAlreadyShipped(0);
        order.setAlreadyShipped(0);
        orderMapper.insert(order);

        //获取新生成的订单号
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        List<Order> listOrder= orderMapper.selectList(queryWrapper);
        Order newOrder = listOrder.get(listOrder.size()-1);
        int orderId = newOrder.getOrderId();

        //将lineItem注入order中
        List<CartItemVO> cartItemVOList = cartVO.getItemVOList();
        int lineNum = 1;
        for(CartItemVO cartItemVO: cartItemVOList){
            LineItem lineItem = new LineItem();
            lineItem.setItemId(cartItemVO.getItemID());
            lineItem.setOrderId(orderId);
            lineItem.setLineNumber(lineNum++);
            lineItem.setQuantity(cartItemVO.getQuantity());
            lineItem.setUnitPrice(cartItemVO.getListPrice());
            order.addLineItem(lineItem);
        }
        //插入lineItem表
        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = (LineItem) order.getLineItems().get(i);
            lineItemMapper.insert(lineItem);
       }
        return CommonResponse.createForSuccessMessage("插入成功");
    }

    @Override
    public CommonResponse<OrderVO> selectOrder(HttpSession session){
        //取得获得购物车的数据
        CommonResponse<CartVO> response = cartService.viewCart(session);
        CartVO cartVO = response.getData();
        //清空session数据
        CartVO newCartVO = new CartVO();
        session.setAttribute("cartVO",newCartVO);
        Order order = new Order();
        session.setAttribute("order",order);
        OrderVO orderVO = new OrderVO();
        session.setAttribute("orderVO",orderVO);

        //获取新生成的订单号
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        List<Order> listOrder= orderMapper.selectList(queryWrapper);
        Order newOrder = listOrder.get(listOrder.size()-1);
        int orderId = newOrder.getOrderId();

        OrderVO newOrderVO = new OrderVO();
        newOrderVO.setOrderId(newOrder.getOrderId());
        newOrderVO.setCardType(newOrder.getCardType());
        newOrderVO.setCardNumber(newOrder.getCreditCard());
        newOrderVO.setOrderDate(newOrder.getOrderDate());
        newOrderVO.setBillFirstName(newOrder.getBillToFirstName());
        newOrderVO.setBillLastName(newOrder.getBillToLastName());
        newOrderVO.setBillAddress1(newOrder.getBillAddress1());
        newOrderVO.setBillAddress2(newOrder.getBillAddress2());
        newOrderVO.setBillCity(newOrder.getBillCity());
        newOrderVO.setBillState(newOrder.getBillState());
        newOrderVO.setBillZip(newOrder.getBillZip());
        newOrderVO.setBillCountry(newOrder.getBillCountry());
        newOrderVO.setShipFirstName(newOrder.getShipToFirstName());
        newOrderVO.setShipLastName(newOrder.getShipToLastName());
        newOrderVO.setShipAddress1(newOrder.getShipAddress1());
        newOrderVO.setShipAddress2(newOrder.getShipAddress2());
        newOrderVO.setShipCity(newOrder.getShipCity());
        newOrderVO.setShipState(newOrder.getShipState());
        newOrderVO.setShipZip(newOrder.getShipZip());
        newOrderVO.setShipCountry(newOrder.getShipCountry());
        newOrderVO.setUserId(newOrder.getUsername());
        newOrderVO.setCourier(newOrder.getCourier());
        newOrderVO.setCartItemVOList(cartVO.getItemVOList());

        return CommonResponse.createForSuccess(newOrderVO);
    }

}
